package sg.edu.nus.iss.cerberus.wizard;

import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.wizard.Wizard;
import org.eclipse.ui.INewWizard;
import org.eclipse.ui.IWorkbench;
import org.eclipse.ui.IWorkbenchPart;
import org.eclipse.core.runtime.*;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.core.runtime.CoreException;
import java.io.*;
import org.eclipse.ui.*;

import sg.edu.nus.iss.cerberus.engine.JUnit4TestClassBuilder;
import sg.edu.nus.iss.cerberus.engine.TestClassDirector;
import sg.edu.nus.iss.cerberus.model.CClass;
import sg.edu.nus.iss.cerberus.model.TestGenerationData;
import sg.edu.nus.iss.cerberus.util.CerberusConstants;

public class NewTestMethodWizard extends Wizard implements INewWizard {

	private NewTestMethodWizardPage page;
	private ISelection selection;
	protected IWorkbench workbench;
	private IWorkbenchPart part;
	CClass classobj;

	public NewTestMethodWizard(IWorkbenchPart part) {
		super();
		setWindowTitle(CerberusConstants.WIZARD_EDITOR_TITLE);
		this.part = part;
		setNeedsProgressMonitor(true);
	}

	/**
	 * Adding the page to the wizard.
	 */

	public void addPages() {
		page = new NewTestMethodWizardPage(selection, part);		
		addPage(page);		
		classobj=page.init((IStructuredSelection)selection);

	}

	
	
	
	/**
	 * This method is called when 'Finish' button is pressed in the wizard. We
	 * will create an operation and run it using wizard as execution context.
	 */
	public boolean performFinish() {
		
		TestGenerationData testGenData=page.populateTestGenData();
		JUnit4TestClassBuilder builder=new JUnit4TestClassBuilder();
		TestClassDirector testdir=new TestClassDirector(builder);
      if(testGenData.getMethodTestData() != null){
		testdir.createTestClass(testGenData);  
		return true;
      }
      else {
    	  return false;
      }
	}

	


	/**
	 * We will accept the selection in the workbench to see if we can initialize
	 * from it.
	 * 
	 * @see IWorkbenchWizard#init(IWorkbench, IStructuredSelection)
	 */
	public void init(IWorkbench workbench, IStructuredSelection selection) {

		this.workbench = workbench;
		this.selection = selection;
		/*
		 * if (selection != null && !selection.isEmpty()) { Object obj =
		 * selection.getFirstElement(); if (obj instanceof IFolder) { IFolder
		 * folder = (IFolder) obj; if (folder.getName().equals("Discounts"))
		 * model.discounted = true; } }
		 */
	}
}